from django.db.models import Q
from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from .paginations import PageNum

from .serializers import *


# SKU商品表管理
# 查询获取sku表列表数据
class SKUView(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer

    pagination_class = PageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword:
            return self.queryset.filter(Q(name__contains=keyword) | Q(caption__contains=keyword))
        else:
            return self.queryset.all()


class SPUSpecView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SPUSpecSerializer

    def get_queryset(self):
        spu_id = self.kwargs.get('pk')
        return self.queryset.filter(spu_id=spu_id)


# # 获取三级分类信息
class SKUCategoriesView(ListAPIView):
    queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    serializer_class = SKUCategoriesSerializer


#
#
# # 获取spu表名称数据
class SPUSimpleView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSerializer


#
#
# # 获取SPU商品规格信息
# class SPUSpecView(ListAPIView):
#     queryset = SPUSpecification.objects.all()
#     serializer_class = SPUSpecSerializer
#
#     def get_queryset(self):
#         # 获取查询字符串参数keyword --> request.query_params.get("keyword")
#         # 在非视图对象中获取请求对象 --> self.request 就是请求对象
#         keyword = self.request.query_params.get('keyword')
#         # 过滤
#         if keyword is '' or keyword is None:
#             return SKU.objects.all()
#         else:
#             return SKU.objects.filter(name=keyword)
#


# SPU表管理
class SPUView(ModelViewSet):
    queryset = SPU.objects.all()
    serializer_class = SPUSerializer

    pagination_class = PageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword:
            return self.queryset.filter(name__contains=keyword)
        else:
            return self.queryset.all()


# 获取品牌信息
class SPUBrandView(ModelViewSet):
    queryset = Brand.objects.all()
    serializer_class = BrandSerializer


# 获取一级分类信息
class GoodsChannelView(ModelViewSet):
    queryset = GoodsCategory.objects.all()
    serializer_class = GoodsChannelSerializer


# 获取二级三级分类信息
class ChannelCategoryView(ModelViewSet):
    queryset = GoodsCategory.objects.all()
    serializer_class = GoodsChannelSerializer

    def get_queryset(self):
        pk = self.kwargs['pk']

        return GoodsCategory.objects.filter(parent=pk)


# 规格表
class SpecificationView(ModelViewSet):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecifcationSerializer
    pagination_class = PageNum


# 规格选项管理
class SpecifiOptionView(ModelViewSet):
    queryset = SpecificationOption.objects.all()
    serializer_class = OptionsSerializer

    pagination_class = PageNum


# 新增/修改：显示规格名称
class SPUOptionView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SPUOptionSerializer


# 图片表
from fdfs_client.client import Fdfs_client
from django.conf import settings

class ImageView(ModelViewSet):
    queryset = SKUImage.objects.all()
    serializer_class = ImageSerializer

    pagination_class = PageNum

    # # 获取sku商品信息
    # def simple(self, request):
    #     data = SKU.objects.all()
    #     ser = SKUImageSimpleSerializer(data, many=True)
    #     return Response(ser.data)

    # # 重写创建方法，保存图片
    # def create(self, request, *args, **kwargs):
    #     # 创建FDFS连接对象
    #     client = Fdfs_client(settings.FASTDFS_PATH)
    #     # 获取前端传递的image文件
    #     data = request.FILES.get('image')
    #     # 上传图片到fdfs
    #     res = client.upload_by_buffer(data.read())
    #     if res['Status'] != 'Upload successed.':
    #         return Response(status=403)
    #     # 获取上传后的路径
    #     image_url = res['Remote file_id']
    #     # 获取sku_id
    #     sku_id = request.data.get('sku')
    #
    #     # sku_id = request.POST.get('sku')
    #     # 保存图片
    #     img = SKUImage.objects.create(sku_id=sku_id, image=image_url)
    #     return Response({
    #         'id': img.id,
    #         'sku': sku_id,
    #         'image': img.image_url
    #     }, status=201)
    #
    # def update(self, request, *args, **kwargs):
    #     # 创建连接对象
    #     client = Fdfs_client(settings.FASTDFS_PATH)
    #     # 获取前端回传的image文件
    #     data = request.FILES.get('image')
    #     sku_id = request.data.get('sku')
    #
    #     if not all([data, sku_id]):
    #         return Response(status=405)
    #     ret = client.upload_by_buffer(data.read())
    #     if ret['Status'] != 'Upload successed':
    #         return Response(status=403)
    #     image_url = ret['Remote file_id']
    #     image_id = kwargs.get('pk')
    #     img_obj = SKUImage.objects.get(pk=image_id)
    #     img_obj.sku_id = sku_id
    #     img_obj.image = image_url
    #     img_obj.save()
    #
    #     return Response({
    #         'id': image_id,
    #         'sku': img_obj.sku_id,
    #         'image': img_obj.image.url
    #     })


# 获取商品id
class ImageSimpleView(ListAPIView):
    queryset = SKU.objects.all()
    serializer_class = SKUImageSimpleSerializer
